home *** CD-ROM | disk | FTP | other *** search
- Path: lrz-muenchen.de!sun2!ua302aa
- From: ua302aa@sun2.lrz-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c
- Subject: Re: help!: An array of strings
- Date: 2 Jan 1996 11:45:42 GMT
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Distribution: world
- Message-ID: <4cb5t6$onb@sparcserver.lrz-muenchen.de>
- References: <Pine.SUN.3.91.960101231746.4185A-100000@parsifal.nando.net>
- NNTP-Posting-Host: sun2.lrz-muenchen.de
-
- pretzel <pretzel@nando.net> writes:
-
- >Is there any way to create and use an array of strings? I am trying to
- >create an adventure game that gets its descriptions from an array. In
- >BASIC the code looks like this....
-
- [BASIC code edited]
-
- >access them. That is how I do it in BASIC, but I can't figure it out in
- >C, since 'char rooms[50][2]' defines an array of single characters. One
- >element in the array might be "You are in a dark alley which reeks of
- >garbage. You feel nervous." so THAT approach certainly is not what I'm
- >looking for.
-
- 1) Yes, there is a way to create and use an array of strings. The
- problem is that C has no built-in sting type like other languages.
- In C, a string is a consecutive area of memory with a sequence
- of characters that is terminated by '\0'.
-
- So, the real problem is how to represent strings in C. There are
- two approaches that are somewhat "natural" and dozends of other
- ones that have advantages and disadvantages in certain situations.
-
- 2) Use either
-
- char *rooms[50][2];
-
- of
-
- char rooms[50][2][128];
-
- two declare an array of arrays of either pointers to char
- or arrays of char.
-
- In the first case, you can easily assign "string constants" to
- the elements of your array:
-
- rooms[32][0] = "You are somewhere and it looks somehow";
-
- In the second case, the arrays of char can be used to store the
- strings, and you can modify them if you have to:
-
- strcpy(rooms[49][1], "You feel somehow");
-
- /* and later: */
-
- strcpy(strstr(rooms[49][1], "somehow"), "dizzy");
-
- Kurt
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
- | ua302aa@sunmail.lrz-muenchen.de
-
-